package com.hanthink.web.controller.app.my;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateTime;
import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.domain.entity.SysDept;
import com.hanthink.common.core.domain.entity.SysRole;
import com.hanthink.common.core.domain.entity.SysUser;
import com.hanthink.common.core.text.Convert;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.system.domain.message.NewsRecordInfo;
import com.hanthink.system.mapper.SysDeptMapper;
import com.hanthink.system.mapper.SysUserMapper;
import com.hanthink.system.mapper.resourcecall.ResourceCallMapper;
import com.hanthink.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.common.utils.DateUtils;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.pc.EmOutgoingQuality;
import com.hanthink.system.domain.pc.EmTask;
import com.hanthink.system.service.equipment.IEmOutgoingQualityService;
import com.hanthink.system.service.pc.IEmTaskService;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

/**
 * 任务质量检查控制类
 *
 * @author hanthink
 */
@RestController
@RequestMapping("/app/my/qualit")
public class EmOutgoingQualityControll extends BaseController {

    @Autowired
    private IEmOutgoingQualityService emOutgoingQualityService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IEmTaskService emTaskService;

    @Autowired
    private ISysDeptService deptService;


    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ResourceCallMapper resourceCallMapper;

    // 查询信息列表
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
            emOutgoingQuality.setSiteKey(loginUser.getUser().getSite().toString());
        }
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
            emOutgoingQuality.setHangameKey(loginUser.getUser().getHangame().toString());
        }
        emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
        List<EmOutgoingQuality> list = emOutgoingQualityService.selectEmOutgoingQualityList(emOutgoingQuality);
        return getDataTable(list);
    }

    @PostMapping("/listQC")
    public TableDataInfo listQC(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        //startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date dates = new Date();
        String batchno = loginUser.getUser().getSite().toString() + format.format(dates) + "01" + "A";
        emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
        List<EmOutgoingQuality> list = emOutgoingQualityService.selectEmOutgoingQualityListQC(batchno, emOutgoingQuality.getIsFinished(), loginUser.getUser().getUserId());
        return getDataTable(list);
    }

    /**
     * @Description:  列表展示  根据emOutgoingQuality 传来的sitekey动态生成当月的batchno
     * @param emOutgoingQuality
     * @return: com.hanthink.common.core.page.TableDataInfo
     * @Author: 唐子越
     * @Date: 2023/7/12 9:42
     */
    @PostMapping("/listQC2")
    public TableDataInfo listQC2(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        //startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        List<EmOutgoingQuality> list;
        emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
        String siteKey = emOutgoingQuality.getSiteKey();
        String batchno = null;
        if (!StringUtils.isEmpty(siteKey)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
            Date dates = new Date();
            batchno = siteKey + format.format(dates) + "01" + "A";

            list = emOutgoingQualityService.selectEmOutgoingQualityListQC(batchno, emOutgoingQuality.getIsFinished(), loginUser.getUser().getUserId());
        }else {
            list = emOutgoingQualityService.selectEmOutgoingQualityListQCNobatch(emOutgoingQuality.getIsFinished(), loginUser.getUser().getUserId());
        }


        return getDataTable(list);
    }

    @PostMapping("/listQB")
    public TableDataInfo listQB(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        //startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date dates = new Date();
        String batchno = loginUser.getUser().getSite().toString() + format.format(dates) + "01" + "B";
        emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
        List<EmOutgoingQuality> list = emOutgoingQualityService.selectEmOutgoingQualityListQC(batchno, emOutgoingQuality.getIsFinished(), loginUser.getUser().getUserId());
        return getDataTable(list);
    }

    @PostMapping("/listQA")
    public TableDataInfo listQA(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        //startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date dates = new Date();
        String batchno = "238" + format.format(dates) + "01" + "C";
        emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
        List<EmOutgoingQuality> list = emOutgoingQualityService.selectEmOutgoingQualityListQC(batchno, emOutgoingQuality.getIsFinished(), loginUser.getUser().getUserId());
        return getDataTable(list);
    }

    @PostMapping("/listLPA")
    public TableDataInfo listLPA(@RequestBody Map<String, Object> map) throws ParseException {
        String batchno = map.get("batchno").toString();
        String isfinished = map.get("isfinished").toString();
        String tasktype = map.get("tasktype").toString();
        List<EmOutgoingQuality> list = emOutgoingQualityService.selectEmOutgoingQualityListbybatchno(batchno, isfinished, tasktype);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }

    // 查询当班完成任务列表
    @PostMapping("/shiftFinishTask")
    public TableDataInfo shiftFinishTask(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        EmTask emTask = new EmTask();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
            emTask.setSiteKey(loginUser.getUser().getSite().toString());
        }
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
            emTask.setHangameKey(loginUser.getUser().getHangame().toString());
        }
        emTask.setStatus("2");
        Date[] date = DateUtils.getShiftDate();
        emTask.setStartTime(date[0]);
        emTask.setEndTime(date[1]);
        List<EmTask> list = emTaskService.selectEmTaskList(emTask);
        return getDataTable(list);
    }

    @PostMapping("/shiftFinishTaskQC")
    public TableDataInfo shiftFinishTaskQC(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        EmTask emTask = new EmTask();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
            emTask.setSiteKey(loginUser.getUser().getSite().toString());
        }
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
            emTask.setHangameKey(loginUser.getUser().getHangame().toString());
        }
        emTask.setStatus("2");
        Date[] date = DateUtils.getShiftDateQC();
        emTask.setStartTime(date[0]);
        emTask.setEndTime(date[1]);
        List<EmTask> list = emTaskService.selectEmTaskList(emTask);
        return getDataTable(list);
    }



    /**
     * @Description: qpp端获取还可以抽取的任务数量
     * @param
     * @return: com.hanthink.common.core.domain.AjaxResult
     * @Author: 唐子越
     * @Date: 2023/7/25 14:49
     */
    @PostMapping("/countCanBeExtracted1")
    public AjaxResult countCanBeExtracted1(){

        EmOutgoingQuality emOutgoingQuality = new EmOutgoingQuality();

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
            emOutgoingQuality.setSiteKey(loginUser.getUser().getSite().toString());
        }
        return emOutgoingQualityService.countCanBeExtracted(emOutgoingQuality);
    }


    /**
     * @Description: web端获取还可以抽取的任务数量
     * @param emOutgoingQuality
     * @return: com.hanthink.common.core.domain.AjaxResult
     * @Author: 唐子越
     * @Date: 2023/7/25 14:50
     */
    @PostMapping("/countCanBeExtracted2")
    public AjaxResult countCanBeExtracted2(@RequestBody EmOutgoingQuality emOutgoingQuality){

        return emOutgoingQualityService.countCanBeExtracted(emOutgoingQuality);
    }

    @PostMapping("/shiftFinishTaskQA")
    public TableDataInfo shiftFinishTaskQA(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        String datestring = format.format(date) + "01";
        List<EmTask> list = emTaskService.selectEmTaskListQA(datestring);
        return getDataTable(list);
    }

    // 主动抽取当前任务检查
    @PreAuthorize("@ss.hasPermi('task:qualit:extract')")
    @PostMapping("/extractShiftTask")
    public AjaxResult extractShiftTask(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
            emOutgoingQuality.setSiteKey(loginUser.getUser().getSite().toString());
        }
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
            emOutgoingQuality.setHangameKey(loginUser.getUser().getHangame().toString());
        }
        emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
        return emOutgoingQualityService.extractEmOutgoingQuality(emOutgoingQuality);
    }

    @PostMapping("/extractShiftTaskQC")
    public AjaxResult extractShiftTaskQC(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
            emOutgoingQuality.setSiteKey(loginUser.getUser().getSite().toString());
            emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
        }
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
            emOutgoingQuality.setHangameKey(loginUser.getUser().getHangame().toString());
        }

        return emOutgoingQualityService.extractEmOutgoingQualityQC(emOutgoingQuality);
    }

    /**
     * @Description: 根据 emOutgoingQuality 中传来的siteKey抽取
     * @param emOutgoingQuality
     * @return: com.hanthink.common.core.domain.AjaxResult
     * @Author: 唐子越
     * @Date: 2023/7/12 9:41
     */
    @PostMapping("/extractShiftTaskQC2")
    public AjaxResult extractShiftTaskQC2(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
        return emOutgoingQualityService.extractEmOutgoingQualityQC(emOutgoingQuality);
    }

    @PostMapping("/extractShiftTaskQA")
    public AjaxResult extractShiftTaskQA(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
        //emOutgoingQuality.setSiteKey(loginUser.getUser().getSite().toString());
        emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
//		}

        return emOutgoingQualityService.extractEmOutgoingQualityQA(emOutgoingQuality);
    }

    @PostMapping("/extractShiftTaskLPA")
    public TableDataInfo extractShiftTaskLPA(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //1、先获取登录人员配置的角色
        //2、再通过角色获取部门
        //3、通过部门获取已完成任务
        for (SysRole roleId : loginUser.getUser().getRoles()) {
            String roleName = roleId.getRoleName();
            Long deptid = loginUser.getUser().getDeptId();
            Long parentdeptid = deptService.selectDeptParentByID(deptid);
            SysDept dept = deptService.selectDeptByName(roleName, parentdeptid);
            if (dept == null) continue;
            emOutgoingQuality.setHangameKey(dept.getDeptName());

            emOutgoingQuality.setSiteKey(parentdeptid.toString());

            if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
                emOutgoingQuality.setHangameKey(loginUser.getUser().getHangame().toString());
            }
            emOutgoingQuality.setUserId(loginUser.getUser().getUserId());
            emOutgoingQualityService.extractEmOutgoingQualityLPA(emOutgoingQuality);
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        return tableDataInfo;
    }

    /**
     * 新增设备任务质量检查
     */
    @Log(title = "设备任务质量检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        return toAjax(emOutgoingQualityService.insertEmOutgoingQuality(emOutgoingQuality));
    }

    /**
     * 修改设备任务质量检查
     */
    @Log(title = "设备任务质量检查", businessType = BusinessType.UPDATE)
    @PostMapping("/updateQuality")
    public AjaxResult edit(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        return toAjax(emOutgoingQualityService.updateEmOutgoingQuality(emOutgoingQuality));
    }

    @PostMapping("/updateQualityQC")
    public AjaxResult editQC(@RequestBody EmOutgoingQuality emOutgoingQuality) {
        //插入消息提醒
        if (!emOutgoingQuality.getEvaluationtask().replace(" ", "").replace(",", "").equals("")) {
            NewsRecordInfo newsRecordInfo = new NewsRecordInfo();
            newsRecordInfo.setCreateTime(DateTime.now().toString("YYYY-MM-dd HH:mm:ss"));
            SysUser user1 = sysUserMapper.selectUserByUserNickName(emOutgoingQuality.getExecuter());
            SysUser user2 = sysUserMapper.selectUserByUserNickName(emOutgoingQuality.getCheckUser());
            newsRecordInfo.setNotingUser(user1.getUserName());
            newsRecordInfo.setSendUser(user2.getUserName());
            newsRecordInfo.setNewsType("任务评价");
            newsRecordInfo.setNewsDesc("设备名称:" + emOutgoingQuality.getEquipName() + ",评价描述:" + emOutgoingQuality.getEvaluationtask().replace(",", ";"));
            resourceCallMapper.insertNewsRecord(newsRecordInfo);
        }
        return toAjax(emOutgoingQualityService.updateEmOutgoingQualityQC(emOutgoingQuality));
    }

}
